/**
 * 
 */
package userLogic;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import encryption.Encrypt;
import userEntity.EmployeeEntity;
import userEntity.UserEntity;
import userService.UserService;
import vehicleEntity.SaleVehicleEntity;
import vehicleService.SaleService;

/**
 * @author Arimarajan
 *
 */
public class UserLogic {

	
	
	/**
	 * This method fetches the details of the user
	 * @param phno
	 * @return
	 * @throws SQLException 
	 */
	public UserEntity fetchUserDetails(String phoneNumber) throws SQLException{
		UserEntity user = new UserEntity();
		ResultSet rs = null;
		UserService service = new UserService();
		try{
			rs = service.fetchUserDetails(phoneNumber);
			if(rs != null && rs.next()){
				user.setPhoneNumber(rs.getString("phoneNumber"));
				user.setName(rs.getString("name"));
				user.setUserType(rs.getString("userType"));
				user.setEmailID(rs.getString("emailId"));
				user.setAddress(rs.getString("address"));
				user.setPincode(rs.getString("pincode"));
				
			}
			else{
				System.out.println("");
			}
		}
		catch(SQLException sqlExce){
			sqlExce.printStackTrace();
			throw sqlExce;
		}
		service.closeConnection();
		return user;
	}
	
	
	
	/**
	 * This method updates the user details
	 * @param user
	 * @return
	 * @throws SQLException 
	 */
	public String updateUserDetails(UserEntity user) throws SQLException{
		
		UserService userservice = new UserService();
		userservice.updateUserDetails(user);
		userservice.transactionCommit();
		return null;
	}
	
	/**
	 * This method verifies that whether the old password 
	 * entered by the user is correct
	 * @param phoneNumber
	 * @param oldPwd
	 * @return
	 * @throws Exception 
	 */
	public boolean verifyOldPassword(String phoneNumber, String oldPwd) throws Exception{
		UserService useserve = new UserService();
		ResultSet resultSet =useserve.fetchOldPassword(phoneNumber);
		UserEntity userEntity = new UserEntity();
		
		if (resultSet!=null && resultSet.next()){
			userEntity.setPwd(resultSet.getString("password"));
		}
		else
		{
			System.out.println("no value returned");
		}
		if(Encrypt.decrypt(userEntity.getPwd()).equals(oldPwd))
		{
			return true;
		}
		return false;
	}
	
	/**
	 * This method updates the user pwd
	 * @param phoneNumber
	 * @param pwd
	 * @return
	 * @throws Exception 
	 */
	public String changePwd(String phoneNumber, String pwd) throws Exception{
		UserService uservice = new UserService();
		uservice.changePwd(phoneNumber, Encrypt.encrypt(pwd));
		return null;
	}
	
	/**
	 * 
	 * @param employeeId
	 * @return
	 */
	public EmployeeEntity fetchEmployeeDetails(int employeeId){
		
		return null;
	}
	
	/**
	 * This method deactivates the user
	 * @param phoneNumber
	 * @return
	 * @throws SQLException 
	 */
	public String deActivate(String phoneNumber) throws SQLException{
		UserService service = new UserService();
		service.deActivate(phoneNumber);
		return null;
	}
	
	/**
	 * This method activates the user
	 * @param phoneNumber
	 * @return
	 * @throws SQLException 
	 */
	public String Activate(String phoneNumber) throws SQLException{
		UserService service = new UserService();
		service.Activate(phoneNumber);
		return null;
	}
	
	
}
